package com.ivt.controller.common;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ivt.core.ex.IvtException;
import com.ivt.core.util.Constants;
import com.ivt.model.Student;
import com.ivt.model.Teacher;
import com.ivt.model.view.Rst;
import com.ivt.service.StudentService;
import com.ivt.service.TeacherService;
import com.ivt.utils.LoginUserHelper;

@Controller
@RequestMapping("/common")
public class LoginoutController {

	@Resource
	private TeacherService teacherService;

	@Resource
	private StudentService studentService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(String username, String password, boolean rememberMe) {
		try {
			LoginUserHelper.login(username, password, rememberMe);
		} catch (AuthenticationException ae) {
			ae.printStackTrace();
		}
		return "redirect:/";
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "alone:common.login";
	}

	@RequestMapping(value = "/doAjaxLogin", method = RequestMethod.POST)
	@ResponseBody
	public Rst doAjaxLogin(@RequestParam(value = "userName") String username,
			@RequestParam(value = "password") String password,
			@RequestParam(value = "rememberMe") boolean rememberMe,
			final HttpServletRequest request) {
		Rst result = new Rst();
		if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
			result.setMessage("请输入用户名或密码");
			return result;
		} else if (StringUtils.isBlank(username)) {
			result.setMessage("用户名不能为空");
			return result;
		} else if (StringUtils.isBlank(password)) {
			result.setMessage("密码不能为空");
			return result;
		}
		try {
			LoginUserHelper.login(username, password, rememberMe);
			result.setCode(Constants.SUCCESS);
			result.setData("/console");
			result.setMessage("登陆成功");
		} catch (AuthenticationException ae) {
			result.setCode(Constants.ERROR);

			if (ae.getCause() instanceof IvtException) {
				result.setMessage(ae.getCause().getMessage());
			} else {
				result.setMessage("用户名或密码错误或账户已失效");
			}
		}
		return result;
	}

	@RequestMapping("/logout")
	public String logout(final HttpServletRequest request) {
		LoginUserHelper.logout();
		HttpSession session = request.getSession();
		session.removeAttribute(Constants.SESSION_STUDENT_KEY);
		session.removeAttribute(Constants.SESSION_TEACHER_KEY);

		return "redirect:/";
	}

	@RequestMapping(value = "/teacher/doAjaxLogin", method = RequestMethod.POST)
	@ResponseBody
	public Rst doTeacherAjaxLogin(
			@RequestParam(value = "userName") String username,
			@RequestParam(value = "password") String password,
			@RequestParam(value = "rememberMe") boolean rememberMe,
			final HttpServletRequest request) {
		Rst result = new Rst();
		if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
			result.setMessage("请输入用户名或密码");
			return result;
		} else if (StringUtils.isBlank(username)) {
			result.setMessage("用户名不能为空");
			return result;
		} else if (StringUtils.isBlank(password)) {
			result.setMessage("密码不能为空");
			return result;
		}
		try {

			Teacher teacher = teacherService.login(username, password);

			HttpSession session = request.getSession();

			session.setAttribute(Constants.SESSION_TEACHER_KEY, teacher);
			result.setCode(Constants.SUCCESS);
			result.setMessage("登陆成功");
			result.setData("/teacher");
		} catch (Exception ae) {
			result.setCode(Constants.ERROR);

			if (ae.getCause() instanceof IvtException) {
				result.setMessage(ae.getCause().getMessage());
			} else {
				result.setMessage("用户名或密码错误或账户已失效");
			}
		}
		return result;
	}

	@RequestMapping("/teacher/logout")
	public Rst teacherLogout(final HttpServletRequest request) {
		Rst rst = new Rst();
		HttpSession session = request.getSession();

		session.removeAttribute(Constants.SESSION_TEACHER_KEY);
		rst.setCode(Constants.SUCCESS);
		rst.setMessage("已成功退出");

		return rst;
	}

	@RequestMapping(value = "/student/doAjaxLogin", method = RequestMethod.POST)
	@ResponseBody
	public Rst doStudentAjaxLogin(
			@RequestParam(value = "userName") String username,
			@RequestParam(value = "password") String password,
			@RequestParam(value = "rememberMe") boolean rememberMe,
			final HttpServletRequest request) {
		Rst result = new Rst();
		if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
			result.setMessage("请输入用户名或密码");
			return result;
		} else if (StringUtils.isBlank(username)) {
			result.setMessage("用户名不能为空");
			return result;
		} else if (StringUtils.isBlank(password)) {
			result.setMessage("密码不能为空");
			return result;
		}
		try {

			Student student = studentService.login(username, password);

			HttpSession session = request.getSession();

			session.setAttribute(Constants.SESSION_STUDENT_KEY, student);
			result.setCode(Constants.SUCCESS);
			result.setMessage("登陆成功");
			result.setData("/student");
		} catch (Exception ae) {
			result.setCode(Constants.ERROR);

			if (ae.getCause() instanceof IvtException) {
				result.setMessage(ae.getCause().getMessage());
			} else {
				result.setMessage("用户名或密码错误或账户已失效");
			}
		}
		return result;
	}

	@RequestMapping("/student/logout/{coursesId}/{teacherId}")
	public String studentLogout(@PathVariable Long coursesId,
			@PathVariable Long teacherId, final HttpServletRequest request) {
		HttpSession session = request.getSession();

		session.removeAttribute(Constants.SESSION_STUDENT_KEY);

		return "redirect:/common/student/" + coursesId + "/sign/" + teacherId;
	}
}
